package com.nbsaas.boot.security.utils;

import com.nbsaas.boot.rest.simple.UserSimple;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.beans.BeanUtils;

public class SubjectUtils {

    public static UserSimple user() {
        Object obj = SecurityUtils.getSubject().getPrincipal();
        if (obj == null) {
            throw new UnauthenticatedException();
        }
        if (obj instanceof UserSimple) {
            return (UserSimple) obj;
        }
        UserSimple result = new UserSimple();
        BeanUtils.copyProperties(obj, result);
        return result;
    }
}
